package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.a_interface.RecyclerViewItemOnClickListener;
import com.daluzy.chmanager.adapter.EmployeeAdapter;
import com.daluzy.chmanager.adapter.StudentAdapter;
import com.daluzy.chmanager.bean.Employee;
import com.daluzy.chmanager.bean.Students;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 学生管理
 */
public class StudentManageActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_student_daluzy);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this);
        rvData.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }
    private void initData() {
        if (loadingDialog != null)
            loadingDialog.show();
        BmobQuery<Students> query = new BmobQuery<>();
        query.findObjects(new FindListener<Students>() {
            @Override
            public void done(List<Students> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size()>0) {
                        StudentAdapter studentAdapter = new StudentAdapter(StudentManageActivity.this, object);
                        studentAdapter.setItemOnClickListener(new RecyclerViewItemOnClickListener<Students>() {
                            @Override
                            public void itemOnClick(Students students, int position) {
                                Intent intent = new Intent(StudentManageActivity.this, StudentInfoActivity.class);
                                intent.putExtra("studentId", students.getObjectId());
                                startActivity(intent);
                            }
                        });
                        rvData.setAdapter(studentAdapter);
                    }
                } else {
                    Toast.makeText(StudentManageActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.iv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_right:
                startActivity(new Intent(StudentManageActivity.this, AddStudentActivity.class));
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
